﻿using UnityEngine;
using UnityEngine.UI;

namespace BF
{
    public class MultiLanguage : MonoBehaviour
    {
        public int key;

        private void Awake()
        {
            if (key <= 0)
            {
                return;
            }

            Text text = GetComponent<Text>();
            if (text == null)
            {
#if DEBUG
                Debug.LogWarning($"[MultiLanguage] [{gameObject.name}], not has Text Component");
#endif
                return;
            }

            if (!MultiLanguageUtil.HasKey(key))
            {
#if DEBUG
                Debug.LogWarning($"[MultiLanguage] [{gameObject.name}], key {key} is not in MultiLanguageUtil");
#endif
                return;
            }

            text.text = MultiLanguageUtil.Get(key);

            ContentSizeFitter fillter = GetComponent<ContentSizeFitter>();
            if (fillter)
            {
                fillter.SetLayoutHorizontal();
                fillter.SetLayoutVertical();
            }
        }

        private void Start()
        {
            if (!Application.isEditor)
            {
                Object.Destroy(this);
            }
        }
    }
}